package com.bnz.service.impl;

import cn.hutool.db.Session;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.mapper.ClassesMapper;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.util.MyBatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {


    //查询所有学生
    @Override
    public List<Student> findAll() {

        try (SqlSession session = MyBatisUtil.getSqlSession()) {

            //查询所有学生
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);

            List<Student> students = mapper.findAll();

            //查询某个学生所在班级
            for (Student student : students) {
                String cname = classesMapper.findById(student.getCid()).getCname();
                student.setCname(cname);
            }

            return students;
        }


    }

    @Override
    public PageResult<Student> findByPage(int page, int pagesize) {
        //开始分页查询
        PageHelper.startPage(page, pagesize);
        //进行查询
        try (SqlSession session = MyBatisUtil.getSqlSession()) {
            //得到mapper
            assert session != null;
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            List<Student> students = mapper.findAll();

            //查询某个学生所在班级
            for (Student student : students) {
                String cname = classesMapper.findById(student.getCid()).getCname();
                student.setCname(cname);
            }
            //将学生集合转换为Ipage
            Page<Student> studentPage = (Page<Student>) students;

            return new PageResult<>(studentPage.getPages(), studentPage.getResult());
        }

    }

    //条件查询带分页
    @Override
    public PageResult<Student> search(int page, int pagesize, StudentVo vo) {
        //开始分页查询
        PageHelper.startPage(page, pagesize);
        //进行查询
        try (SqlSession session = MyBatisUtil.getSqlSession()) {
            //得到mapper
            assert session != null;
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            List<Student> students = mapper.search(vo);

            //查询某个学生所在班级
            for (Student student : students) {
                String cname = classesMapper.findById(student.getCid()).getCname();
                student.setCname(cname);
            }
            //将学生集合转换为Ipage
            Page<Student> studentPage = (Page<Student>) students;

            return new PageResult<>(studentPage.getPages(), studentPage.getResult());
        }
    }

    //添加
    @Override
    public void insert(Student student) {

        try(SqlSession session=MyBatisUtil.getSqlSession()){

            StudentMapper mapper=session.getMapper(StudentMapper.class);

            mapper.insert(student);

        }


    }

    //5. 修改学生
    @Override
    public void update(Student student) {
        try(SqlSession session = MyBatisUtil.getSqlSession()){
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            mapper.update(student);
        }
    }
}
